package com.xianyun.book;

import com.alibaba.cola.domain.ApplicationContextHelper;
import com.xianyun.book.common.core.boot.EventRegister;
import com.xianyun.book.common.core.event.EventHandler;
import com.xianyun.book.common.core.event.EventHandlerI;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import javax.annotation.Resource;
import java.util.Map;

/**
 * @author ytt
 */
@ComponentScan(value = {"com.alibaba.cola","com.xianyun.book"})
@SpringBootApplication(scanBasePackages = { "com.xianyun.book", "com.alibaba.cola" })
@MapperScan("com.xianyun.book.gatewayimpl.database")
public class UsedBookApplication {
    @Autowired
    private ApplicationContext applicationContext;

    @Resource
    private EventRegister eventRegister;

    @Bean
    public void init() {
        applicationContext = ApplicationContextHelper.getApplicationContext();
        Map<String, Object> eventHandlerBeans = applicationContext.getBeansWithAnnotation(EventHandler.class);
        eventHandlerBeans.values().forEach(
                eventHandler -> eventRegister.doRegistration((EventHandlerI) eventHandler)
        );
    }

    public static void main(String[] args) {
        SpringApplication.run(UsedBookApplication.class, args);
    }

}
